import json
from flask import Blueprint, request
from time import sleep, time

from gua.Gua01 import Gua01


class GuaWebApi:
    def __init__(self):
        self.service = [
            Gua01(),
        ]

    def get_service(self):
        index = request.args.get('gua', 0)
        return self.service[int(index)]

    def nav(self):
        data = self.get_service().nav()
        return json.dumps({
            'error_code': 0,
            'message': 'ok',
            'data': data,
        })

    def list(self):
        path = request.args.get('path')
        page = request.args.get('page')
        if not path:
            path = self.get_service().default_page
        if not path:
            page = '1'

        data = self.get_service().list(path.format(page))
        return json.dumps({
            'code': 0,
            'msg': 'ok',
            'data': data,
        })


controller = GuaWebApi()
gua = Blueprint('gua_01', __name__)
gua.add_url_rule('/gua_01/nav', view_func=controller.nav, methods=['GET', 'POST'])
gua.add_url_rule('/gua_01/list', view_func=controller.list, methods=['GET', 'POST'])

if '__main__' == __name__:
    import os

    # print(melon_list())
    r = os.path.getmtime('../cache/7fe96495ea8842f1afdaf29dc4dfb2a1.html')
    print(time() - r)
    r = os.stat('../cache/7fe96495ea8842f1afdaf29dc4dfb2a1.html')
    print(r.st_ctime)  # 创建时间
    print(r.st_mtime)  # 最后一次修改的时间
